<!-- 同步云端数据页面 -->
<template>
  <a-skeleton active :loading="loading" :paragraph="{rows: 17}">
    <a-card :bordered="false">

      <a-button @click="syncAll" type="primary" :loading="false" style="float: right; margin-bottom: 10px">一键拉取</a-button>

      <a-table
        rowKey="id"
        size="middle"
        bordered
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="tableLoading"
        style="margin-top: 20px;">
        <template slot="param" slot-scope="text, record">
          {{ text }}
        </template>
        <template slot="text" slot-scope="text, record">
          {{ text }}
        </template>
        <template slot="value" slot-scope="text, record">
          {{ text }}
        </template>
        <template slot="lastTime" slot-scope="text, record">
          <span v-if="record.newestRecord">
            {{record.newestRecord.updateTime}}
          </span>
        </template>
        <template slot="status" slot-scope="text, record">
          <span v-if="record.newestRecord">
            <span v-if="record.newestRecord.result==1">
              <div style="color: green">成功</div>
            </span>
            <span v-else-if="record.newestRecord.result==2">
              <div style="color: red">失败</div>
            </span>
            <span v-else>
              <div style="color: yellow">未知</div>
            </span>
          </span>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="syncSingle(record)">数据拉取</a>
        </span>
      </a-table>
    </a-card>
  </a-skeleton>
</template>
<script>
  import moment from 'moment'
  import { getAction } from '@/api/manage'
  import {postAction} from "../../api/manage";

  moment.locale('zh-cn')

  export default {
    data() {
      return {
        time: '',
        loading: false,
        tableLoading: true,
        columns: [
         {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          // width: "5%",
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },{
          title: '数据类型',
          // width: '10%',
          align: "center",
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' }
        }, {
          title: '说明',
          // width: '20%',
          dataIndex: 'description',
          scopedSlots: { customRender: 'description' }
        }, {
          title: '数据标志',
          align: "center",
          // width: '30%',
          dataIndex: 'value',
          scopedSlots: { customRender: 'value' }
        },{
            title: '状态',
            // width: '5%',
            align: "center",
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
        },{
            title: '最后更新时间',
            // width: '30%',
            align: "center",
            dataIndex: 'newestRecord',
            scopedSlots: { customRender: 'lastTime' }
        },{
          title: '操作',
          dataIndex: 'action',
          scopedSlots: {customRender: 'action'},
          align: 'center',
          // width: 60
        }],
        dataSource: [
          {
            "name":"zhans",
            "description":"zs_des",
            "value":"dddd"
          },
          {
            "name":"lisi",
            "description":"ls_des",
            "value":"sddf"
          },
        ],

        moreInfo: {}
      }
    },
    mounted() {
      console.log("mounted");
      // this.loadTomcatInfo()
      this.loadSyncInfo();
    },
    methods: {
      syncAll(){
        var that=this;
        this.tableLoading=true;
        postAction("sys/dataSync/syncAllData",{})
                .then((res) => {
                  if (res.success) {
                    that.$message.success(res.message);
                  } else {
                    that.$message.warning("同步失败: "+res.message)
                  }
                }).catch((e) => {
                  console.error(e)
                  this.$message.error('同步数据失败')
                }).finally(() => {
                  that.loadSyncInfo();
                  this.loading = false
                  this.tableLoading = false
                });
      },

      syncSingle(record){
        var that=this;
        this.tableLoading = true;
        console.log(record.value);
        postAction("sys/dataSync/syncDataItem",{"dataName":record.value})
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
              } else {
                that.$message.warning(res.message)
              }
            }).catch((e) => {
              console.error(e)
              this.$message.error('同步数据失败')
            }).finally(() => {
              that.loadSyncInfo();
              this.loading = false
              this.tableLoading = false
            });
      },

      async getStatus() {
        let data;
        await getAction('sys/dataSync/status', {}).then(res => {
          if (res.success) {
            data = res.result;
          } else {
            data = -1;
          }
        }).catch(error => {
          console.log(error);
          data = -1;
        })
        return data;
      },

      loadSyncInfo(){
        this.tableLoading = true;
        this.time = moment().format('YYYY年MM月DD日 HH时mm分ss秒');
        Promise.all([
            getAction('sys/dataSync/newestSyncInfo')
          ]).then((res) => {
          let info = []
          res.forEach((value, id) => {
            if(value && value.success){
              value.result.forEach((item,index)=>{
                 // info.push({id:item.id, name:item.name, description:item.description,value:item.value});
                info.push(item);
              })
            }else{
              console.log("解析数据失败");
            }
          })
          this.dataSource = info

        }).catch((e) => {
              console.error(e)
              this.$message.error('获取服务器信息失败')
            })
           .finally(() => {
              this.loading = false
              this.tableLoading = false
            })
        },

      convert(value, type) {
        if (type === Number) {
          return Number(value * 100).toFixed(2)
        } else if (type === Date) {
          return moment(value * 1000).format('YYYY-MM-DD HH:mm:ss')
        }
        return value
      }
    }
  }
</script>
<style></style>
